local skel = fk.CreateSkill {
  name = "emo__woshou",
}

Fk:loadTranslationTable{
  ["emo__woshou"] = "握手",
  [":emo__woshou"] = "游戏开始时，你可与一名其他角色依次获得随机转换技。你或其发动转换技后，令对方切换转化技状态至相同。",

  ["#emo__woshou-choose"] = "令你与一名其他角色获得随机转换技",
  ["@@emo__woshou"] = "握手",

  ["$emo__woshou1"] = "太好听了吧！",
  ["$emo__woshou2"] = "你唱歌真的好好听啊，简直就是天籁！",
  ["$emo__woshou3"] = "真爱！这是真爱啊！",
}

skel:addEffect(fk.GameStart, {
  anim_type = "switch",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_cost = function (self, event, target, player, data)
    local tos = player.room:askToChoosePlayers(player, {
      min_num = 1, max_num = 1, targets = player.room:getOtherPlayers(player, false), skill_name = skel.name,
      prompt = "#emo__woshou-choose",
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local exclude = {}
    for _, p in ipairs(room.players) do
      for _, s in ipairs(p:getSkillNameList()) do
        if Fk.skills[s]:hasTag(Skill.Switch) then
          table.insertIfNeed(exclude, s)
        end
      end
    end
    local skills = {}
    for _, g in ipairs (room.general_pile) do
      for _, s in ipairs(Fk.generals[g]:getSkillNameList()) do
        if Fk.skills[s]:hasTag(Skill.Switch) and not table.contains(exclude, s) then
          table.insertIfNeed(skills, s)
        end
      end
    end
    if #skills < 2 then return end
    skills = table.random(skills, 2)
    room:setPlayerMark(player, "@@emo__woshou", 1)
    room:setPlayerMark(to, "@@emo__woshou", 1)
    room:handleAddLoseSkills(player, skills[1])
    room:handleAddLoseSkills(to, skills[2])
  end,
})

skel:addEffect(fk.AfterSkillEffect, {
  can_refresh = function(self, event, target, player, data)
    if target == player and player:getMark("@@emo__woshou") ~= 0 then
      local skill = data.skill
      return skill and not skill.is_delay_effect and skill:hasTag(Skill.Switch)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local state = player:getSwitchSkillState((data.skill:getSkeleton() or data.skill).name)
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if p:getMark("@@emo__woshou") ~= 0 then
        for _, skill in ipairs(p:getSkillNameList()) do
          if Fk.skills[skill]:hasTag(Skill.Switch) and p:getSwitchSkillState(skill) ~= state then
            room:setPlayerMark(p, MarkEnum.SwithSkillPreName .. skill, state)
            room:doAnimate("InvokeSkill", {
              name = skill,
              player = p.id,
              skill_type = "switch",
            })
            p:setSkillUseHistory(skill, p:usedSkillTimes(skill, Player.HistoryGame), Player.HistoryGame)
            room:delay(200)
          end
        end
      end
    end
  end,
})

return skel
